<template>
<Row class="layout-content-body">
    <dtable :columnsList="columns" 
    v-model="dataAll" :totals="total"
     :busyName="busyName" :busyTable="busyTable" 
     add check op delCheck flowstart @refresh="getall()" 
     @add="add" @del="del" @edit="edit" 
     @delList="delList" :loading="loading"></dtable>
    <Modal title="预约信息" 
    v-model="addFlag" width="700" :mask-closable="false" @on-ok="getFenju">
        <Form ref="editForm" :model="formItem" :label-width="80" :rules="ruleValidate">
            <Row :gutter="16">
                <Col span="12">
                <Form-item label="案件名称" prop="Title">
                    <Input v-model="formItem.Title" placeholder="请输入案件名称"></Input>
                </Form-item>
                </Col>
                <Col span="12">
                <Form-item label="案件编号" prop="No">
                    <Input v-model="formItem.No" placeholder="请输入案件编号"></Input>
                </Form-item>
                </Col>
            </Row>
            <FormItem label="检验类型" >
                <Select v-model="formItem.JdType"  @on-change="jyTypeChange">
                  <Option v-for="item in jyType" :value="item.value" :key="item.value">{{ item.name }}</Option>
                </Select>
            </FormItem>
            <FormItem label="分类" >
                <Select v-model="formItem.Category"  >
                  <Option v-for="item in jyCategory" :value="item.value" :key="item.value">{{ item.name }}</Option>
                </Select>
            </FormItem>
            <Row :gutter="16">
                <Col span="12">
                <Form-item label="手机号码" prop="Phone">
                    <Input v-model="formItem.Phone" placeholder="请输入手机号码"></Input>
                </Form-item>
                </Col>
                <Col span="12">
                <FormItem label="是否初检">
                    <RadioGroup v-model="formItem.First">
                        <Radio v-for="item in chujianArray" :label="item.value" :key="item.value">{{item.name}}</Radio>
                    </RadioGroup>
                </FormItem>
                </Col>
            </Row>
            <Row :gutter="16">
                <Col span="12">
                <FormItem label="预约日期">
                    <DatePicker type="date" :options="dateoptions" :start-date="new Date()" v-model="formItem.Date"></DatePicker>
                </FormItem>
                </Col>
                <Col span="12">
                <FormItem label="预约时间">
                    <Select v-model="formItem.Time">
                        <Option v-for="(item,idx) in timeList"  :value="item" :key="idx">{{ item }}</Option>
                      </Select>
                </FormItem>
                </Col>
            </Row>
            <Form-item label="物证信息" prop="Info">
                <Input v-model="formItem.Info" placeholder="请输入物证信息"></Input>
            </Form-item>
        </Form>
    </Modal>
     <Modal v-model="termFlag" width="700" @on-ok="next" ok-text="同意" cancel-text="拒绝">
              <p slot="header" style="color:#f60;text-align:center">
                <Icon type="information-circled"></Icon>
                <span>服务条款</span>
              </p>
              <p v-html="content"></p>
        </Modal>
</Row>
</template>

<script>
import { timeFormat, currency } from '@/common/filter'
import dtable from '@/components/table/etable.vue' // 采用biztable
import moment from 'moment'
import { valid } from 'semver'
export default {
    data () {
        return {
            termFlag: false,
            loading: false, // 数据刷新遮罩层
            busyName: '案件预约',
            busyTable: 'sf_case',
            columns: [
        // 多选框
                {
                    title: '案件名称',
                    key: 'Title',
                    sortable: true
                },
                {
                    title: '案件编号',
                    key: 'No',
                    sortable: true
                },
                {
                    title: '单位',
                    key: 'DeptName',
                    sortable: true
                },
                {
                    title: '是否初检',
                    key: 'First',
                    sortable: true,
                    render: (h, { row }) => {
                        let str = row.First == 1 ? '是' : '否'
                        return h('span', {}, str)
                    }
                },
                {
                    title: '鉴定类型',
                    key: '',
                    sortable: true,
                    render: (h, { row }) => {
                        let str = ''
                        for (let i = 0; i < this.jyType.length; i++) {
                            if (row.JyType == this.jyType[i].value) {
                                str = this.jyType[i].name
                            }
                        }
                        return h('span', {}, str)
                    }
                },
                {
                    title: '分类',
                    key: 'Category',
                    sortable: true,
                    render: (h, { row }) => {
                        let str = ''
                        for (let i = 0; i < this.jyType.length; i++) {
                            if (row.JyType == this.jyType[i].value) {
                                let category = this.jyType[i].children
                                for (let j = 0; j < category.length; j++) {
                                    if (row.Category == category[j].value) {
                                        str = category[j].name
                                    }
                                }
                            }
                        }
                        return h('span', {}, str)
                    }
                },
                {
                    title: '送检人',
                    key: 'Creater',
                    sortable: true
                },
                {
                    title: '填写时间',
                    key: 'Ctime',
                    sortable: true,
                    render: (h, params) => {
                        return h('div', [h('strong', timeFormat(params.row.Ctime))])
                    }
                }
            ], // 表格样式
            ruleValidate: {
                JyType: [
          { required: true, message: '请选择鉴定类别', trigger: 'change' }
                ],
                Category: [
          { required: true, message: '请选择类型', trigger: 'change' }
                ],
                Title: [
                    {
                        required: true,
                        message: '请填写案件名称',
                        trigger: 'blur'
                    }
                ],
                No: [
                    {
                        required: true,
                        message: '请填写案件编号',
                        trigger: 'blur'
                    }
                ],
                Phone: [
                    {
                        required: true,
                        message: '请填写手机号码',
                        trigger: 'blur'
                    }
                ],
                Info: [
                    {
                        required: true,
                        message: '请填写物证信息',
                        trigger: 'blur'
                    }
                ]
            }, // 数据编辑表单验证规则
            dataAll: [], // 表格数据来源
            templist: [],
            dataShow: true, // 是否显示表格数据查看表单
            showTemp: false,
            total: 0,
            orderData: {
                edit: false,
                temp: ''
            },
            dateoptions: {
                disabledDate (date) {
                    return date && date.valueOf() < Date.now() - 86400000
                }
            },
            addFlag: false,
            jyType: [
                {
                    name: '痕迹',
                    value: 0,
                    children: [
                        {
                            name: '手印',
                            value: 0
                        },
                        {
                            name: '足迹',
                            value: 1
                        },
                        {
                            name: '工具',
                            value: 2
                        },
                        {
                            name: '枪弹',
                            value: 3
                        },
                        {
                            name: '特殊',
                            value: 4
                        },
                        {
                            name: '爆炸',
                            value: 5
                        },
                        {
                            name: '其它',
                            value: 6
                        }
                    ]
                },
                {
                    name: '法医',
                    value: 1,
                    children: [
                        {
                            name: '尸体',
                            value: 0
                        },
                        {
                            name: '活体',
                            value: 1
                        },
                        {
                            name: '其它',
                            value: 2
                        }
                    ]
                },
                {
                    name: 'DNA',
                    value: 2,
                    children: [
                        {
                            name: '案件检验',
                            value: 0
                        },
                        {
                            name: '违法犯罪人员血样',
                            value: 1
                        },
                        {
                            name: '其它',
                            value: 2
                        }
                    ]
                },
                {
                    name: '理化',
                    value: 3,
                    children: [
                        {
                            name: '毒品',
                            value: 0
                        },
                        {
                            name: '毒物',
                            value: 1
                        },
                        {
                            name: '微量物证',
                            value: 2
                        },
                        {
                            name: '酒精含量',
                            value: 3
                        },
                        {
                            name: '其它',
                            value: 4
                        }
                    ]
                },
                {
                    name: '文检',
                    value: 4,
                    children: [
                        {
                            name: '笔记',
                            value: 0
                        },
                        {
                            name: '印章',
                            value: 1
                        },
                        {
                            name: '其他',
                            value: 2
                        }
                    ]
                },
                {
                    name: '电子物证',
                    value: 5,
                    children: [
                        {
                            name: '手机',
                            value: 0
                        },
                        {
                            name: '电脑',
                            value: 1
                        },
                        {
                            name: '存储设备',
                            value: 2
                        },
                        {
                            name: '其它',
                            value: 3
                        }
                    ]
                }
            ],
            timeList: [
                '09:00',
                '09:15',
                '09:30',
                '09:45',
                '10:00',
                '10:15',
                '10:30',
                '10:45',
                '11:00',
                '11:15',
                '11:30',
                '13:00',
                '13:15',
                '13:30',
                '13:45',
                '14:00',
                '14:15',
                '14:30',
                '14:45',
                '15:00',
                '15:15',
                '15:30',
                '15:45',
                '16:00',
                '16:15',
                '16:30'
            ],
            jyCategory: [],
            formItem: {
                JdType: 0,
                Category: 0,
                First: 1,
                Phone: '',
                Date: new Date(),
                Time: '09:00',
                Count: 1,
                Info: '',
                Title: '',
                No: '',
                IsAdditional: false
            },
            chujianArray: [
                {
                    name: '是',
                    value: 1
                },
                {
                    name: '否',
                    value: 0
                }
            ],
            content: ''
        }
    },
    components: {
        dtable
    },
    computed: {
        userInfo () {
            return this.$store.state.app.userInfo
        }
    },
    watch: {},
    methods: {
        getFenju () {
            let array = [
                {
                    name: '痕迹',
                    value: 0
                },
                {
                    name: '法医',
                    value: 1
                },
                {
                    name: 'DNA',
                    value: 2
                },
                {
                    name: '理化',
                    value: 3
                },
                {
                    name: '文检',
                    value: 4
                },
                {
                    name: '电子物证',
                    value: 5
                },
                {
                    name: '申明',
                    value: 6
                },
                {
                    name: '服务条款',
                    value: 7
                }
            ]
            let params = array[0]
            for (let i = 0; i < array.length; i++) {
                if (array[i].value == this.formItem.JdType) {
                    params = array[i]
                }
            }
            this.getSfDescByDept(params)
        },
        getSfDescByDept (dept) {
            this.$api.GetSfTermByType(dept).then(res => {
                if (res.code == 0) {
                    this.content = res.Data['Content']
                    this.termFlag = true
                }
            })
        },
        next () {
            this.jdOk()
        },
        jdQueRen () {
            let times = this.formItem.Time.split(':')
            this.formItem.Date.setHours(times[0])
            this.formItem.Date.setMinutes(times[1])
            if (moment() > moment(this.formItem.Date)) {
                return true
            }
            return false
        },
        jdOk () {
            this.$refs['editForm'].validate(valid => {
                if (valid) {
                    if (this.jdQueRen()) {
                        this.$Message.error('无效的时间，请重新选择！')
                        return
                    }

                    this.$api.PostCase(this.formItem).then(res => {
                        if (res.code == 0) {
                            this.getall()
                            this.$Message.success('保存成功!')
                            this.$refs['editForm'].resetFields()
                        }
                    })
                } else {
                    this.$Message.error('请补充信息!')
                }
            })
      // this.$api.CreateSfyy(this.formItem).then(res => {
      //   if (res.code == 0) {
      //     this.getall();
      //   }
      // });
        },
        jyTypeChange (idx) {
      // console.log(idx);
            for (let i = 0; i < this.jyType.length; i++) {
                if (this.jyType[i].value == idx) {
                    this.jyCategory = this.jyType[i].children
                }
            }
        },
        getall () {
            this.loading = true
            this.$api.GetSfCases().then(res => {
                if (!res.error) {
                    console.log(res.Data)
                    this.dataAll = res.Data
                    if (this.dataAll) {
                        this.total = this.dataAll.length
                    }
                } else {
                    this.$Message.error(res.message)
                }
            })
            this.$api.getTemp().then(res => {
                if (!res.error) {
                    this.templist = res.Data
                } else {
                    this.$Message.error(res.message)
                }
            })
            this.loading = false
        },
        edit (index) {
            console.log(index.Id)
            this.orderData.order = index
            this.orderData.edit = true
            this.$router.push({
                name: '采购订单明细',
                params: {
                    orderData: this.orderData
                }
            })
        },
        add () {
            this.addFlag = true
            this.jyTypeChange(this.formItem.JdType)
      // this.formItem.JdType = 0;
        },
        del (row) {
            this.$api.deleteOrder(row).then(res => {
                if (!res.error) {
                    this.getall()
                    this.$Message.success(res.message)
                } else {
                    this.$Message.error(res.message, 4)
                }
            })
        },
        delList (row) {
            console.log(row)
            this.$api.DeleteCaseList(row).then(res => {
                if (!res.error) {
                    this.getall()
                    this.$Message.success(res.message)
                } else {
                    this.$Message.error(res.message, 4)
                }
            })
        }
    },
    mounted () {
    // this.jyTypeChange(0);
        this.formItem.Phone = this.userInfo.Phone
    // this.getall();
    }
}
</script>
